<template>
  <div class="container">
    <el-dialog :title="isTitle" :visible="dialogVisible" @close='close' >
      <el-form :model="dataEdit"  label-width="100px"  ref="form" :rules="formRules">
        <el-form-item label="文章标题" prop="title">
          <el-input
            v-model="dataEdit.title"
            placeholder="请输入文章标签"
          ></el-input>
        </el-form-item>
        <el-form-item label="文章内容：" prop="articleBody">
         <quill-editor v-model="dataEdit.articleBody" :options="editorOptions"></quill-editor>
        </el-form-item>
        <el-form-item label="视频地址：" prop="videoURL">
          <el-input
            v-model="dataEdit.videoURL"
            placeholder="请输入视频地址"
          ></el-input>
        </el-form-item>
      </el-form>
        <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button @click="confirm()" type="primary">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { update, add } from '@/api/hmmm/articles.js'

export default {
  name: 'ArticlesAdd',
  props: {
    data: {
      default: () => {},
      type: Object
    },
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    isTitle () {
      return this.data.id ? '编辑文章' : '添加文章'
    }
  },

  // created () {
  //   this.open()
  // },
  data () {
    return {
      dataEdit: {
        id: null,
        title: null,
        articleBody: null,
        videoURL: null
      },
      formRules: {
        title: [
          { required: true, message: '请输入文章标题', tirgger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '请输入文章内容', tirgger: 'blur' }
        ]
      },
      editorOptions: {
        placeholder: '',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            [{ list: 'ordered' }, { list: 'bullet' }],
            ['blockquote'],
            ['code-block', 'image', 'link']
          ]
        }
      }

    }
  },
  methods: {
    async confirm () {
      console.log(this.data)
      this.$refs.form.validate(async valid => {
        // if (!valid) return
        if (this.dataEdit.id) {
          console.log(this.data, 9666)
          this.dataEdit.id = this.data.id
          console.log(this.dataEdit, 666)
          await update(this.dataEdit)
        } else {
          await add(this.dataEdit)
        }
        this.$message.success('操作成功')
        this.close()
        this.$emit('updateList')
      })
    },
    open () {
      if (this.data.id) {
        const { id, title, articleBody, videoURL } = this.data
        this.dataEdit = { id, title, articleBody, videoURL }
        console.log(this.dataEdit, 9444)
      } else {
        this.dataEdit = {
          id: null,
          title: null,
          articleBody: null,
          videoURL: null
        }
      }
    },
    // 关闭对话框
    close () {
      this.$emit('update:dialogVisible', false)
      this.$nextTick(() => {
        this.$refs.form.resetFields()
      })
    }
  }
}
</script>

<style scoped lang='less'></style>
